using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine.Events;
using UnityEngine.Networking;

namespace QGameFramework.Web
{
    public class WebComponent : BaseComponent
    {
        public override void OnInit()
        {
            base.OnInit();
        }

        #region 外部调用函数

        /// <summary>
        /// 从服务器获取信息
        /// </summary>
        /// <param name="HeadInfo">请求头文件字典</param>
        /// <param name="url">服务器地址</param>
        /// <param name="CallBack">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        public void GetFromServer<T>(Dictionary<string, string> HeadInfo, string url, UnityAction<T, UnityWebRequest> CallBack = null) where T : new()
        {
            StartCoroutine(IE_GetFromServer(HeadInfo, url, CallBack));
        }

        /// <summary>
        /// 上传信息到服务器
        /// </summary>
        /// <param name="headInfo">请求头文件字典</param>
        /// <param name="url">服务器地址</param>
        /// <param name="msg">消息</param>
        /// <param name="CallBack">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        public void Post2Server<T>(Dictionary<string, string> headInfo, string url, string msg, UnityAction<T, UnityWebRequest> CallBack = null) where T : new()
        {
            StartCoroutine(IE_Post2Server(headInfo, url, msg, CallBack));
        }


        public void Put2Server()
        {
        }

        public void AbortFromServer()
        {
        }

        public void HeadFromServer()
        {
        }

        #endregion


        #region 私有函数

        /// <summary>
        /// 从服务器获取信息
        /// </summary>
        /// <param name="headInfo">请求头文件字典</param>
        /// <param name="url">服务器地址</param>
        /// <param name="CallBack">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        /// <returns>空</returns>
        private IEnumerator IE_GetFromServer<T>(Dictionary<string, string> headInfo, string url, UnityAction<T, UnityWebRequest> CallBack = null)
            where T : new()
        {
            using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
            {
                foreach (var key in headInfo.Keys)
                {
                    webRequest.SetRequestHeader(key, headInfo[key]);
                }

                yield return webRequest.SendWebRequest();
                if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
                {
                    LogKit.E("请求失败!!请检查网络!!");
                }
                else
                {
                    LogKit.I("请求正常，进行下一操作");
                    T t = new T();
                    CallBack?.Invoke(t, webRequest);
                }
            }
        }

        /// <summary>
        /// 上传信息到服务器
        /// </summary>
        /// <param name="headInfo">请求头文件字典</param>
        /// <param name="url">服务器地址</param>
        /// <param name="msg">消息</param>
        /// <param name="callBack">回调函数</param>
        /// <typeparam name="T">泛型</typeparam>
        /// <returns>空</returns>
        private IEnumerator IE_Post2Server<T>(Dictionary<string, string> headInfo, string url, string msg, UnityAction<T, UnityWebRequest> CallBack = null)
            where T : new()
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);
            using (UnityWebRequest webRequest = UnityWebRequest.Post(url, msg))
            {
                foreach (var key in headInfo.Keys)
                {
                    webRequest.SetRequestHeader(key, headInfo[key]);
                }

                webRequest.uploadHandler = new UploadHandlerRaw(bytes);
                yield return webRequest.SendWebRequest();
                if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
                {
                    LogKit.E("请求失败!!请检查网络!!");
                }
                else
                {
                    LogKit.I("请求正常，进行下一操作");
                    T t = new T();
                    CallBack?.Invoke(t, webRequest);
                }
            }
        }

        #endregion
    }
}